﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Krist_em_up.Plateforms;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Krist_em_up.Level
{
    public class Parser
    {
        public ContentManager Content;
        public Parser(ContentManager manager)
        {
            Content = manager;
        }
        /// <summary>
        /// Parse the level file and return the corresponding Plateform array
        /// </summary>
        /// <param name="path"></param>
        /// <param name="game"></param>
        /// <returns></returns>
        public Plateform[,] parseLevel(string path, Kristemup game)
        {
            int width;
            Plateform[,] level;
            List<string> lines = new List<string>();
            Vector2 end;
            Vector2 spawn = end = Vector2.Zero;

            using (StreamReader reader = new StreamReader(path))
            {
                string line = reader.ReadLine();
                width = line.Length;
                while (line != null)
                {
                    lines.Add(line);
                    if (line.Length != width)
                        throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count));
                    line = reader.ReadLine();
                }
            }
            level = new Plateform[width, lines.Count];

            // Loop over every tile position,
            for (int y = 0; y < lines.Count; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    // to load each tile.
                    char c = lines[y][x];
                    switch (c)
                    {
                        case 'x':
                            level[x, y] = new Plateform(Content.Load<Texture2D>("block1"), TileCollision.Impassable);
                            break;
                        case '.':
                            level[x, y] = new Plateform(null, TileCollision.Passable);
                            break;
                        case 'w':
                            level[x, y] = new Plateform(null, TileCollision.EndOfLevel);
                            break;
                        case '1':
                            level[x, y] = new Plateform(null, TileCollision.Passable);
                            game.SpawnK(x, y);
                            break;
                        case 'E':
                            level[x, y] = new Plateform(null, TileCollision.Passable);
                            game.SpawnE(x, y);
                            break;
                        case 'P':
                            level[x, y] = new Plateform(null, TileCollision.Passable);
                            game.SpawnP(x, y);
                            break;
                        case 'S':
                            level[x, y] = new Plateform(null, TileCollision.Passable);
                            game.SpawnS(x, y);
                            break;
                        default:
                            break;
                    }
                }
            }

            //Map map = new Map(level, spawn, end);
            //return map;
            return level;
        }
    }
}
